package com.googlecode.xapper

import static org.junit.Assert.*
import static org.hamcrest.Matchers.*

import nu.xom.*

import com.googlecode.xapper.ReadyMades
import org.junit.Test


public class TestReadyMades{

	def context = new XPathContext("html", "http://www.w3.org/1999/xhtml");

	@Test
	public void shouldCreateHeadingTag() {
		Element heading = ReadyMades.heading(1)
		
		def nodes = heading.query('/html:h1', context)
		
		assertThat(nodes.size(), is(1))
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void shouldRejectAHeadingLevelGreaterThanSix() {
		ReadyMades.heading(7)
	}
	
	@Test
	public void shouldCreateAParagraph() {
		def hello = "Hello World"
		Element paragraph = ReadyMades.paragraph(hello)
		
		def nodes = paragraph.query('/html:p', context)
		
		assertThat(nodes.size(), is(1))
		assertThat(nodes.get(0).getValue(), is(equalTo(hello)))
	}
	
	@Test
	public void shouldCreateAHeadingWithText() {
		def hello = "Hello World"
		
		Element heading = ReadyMades.heading(2, hello)
		
		def nodes = heading.query("/html:h2", context)
		
		assertThat(nodes.size(), is(1))
		assertThat(nodes.get(0).getValue(), is(equalTo(hello)))
	}
	
	@Test
	public void shouldCreateAnEmptyParagraph() {
		Element paragraph = ReadyMades.paragraph()
		
		assertThat(paragraph.query("/html:p", context).size(), is(1))
	}
	
	@Test
	public void shouldCreateADiv() {
		Element div = ReadyMades.div()
		
		assertThat(div.query("/html:div", context).size(), is(1))
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void shouldRejectAHeadingLevelLessThanOne() {
		ReadyMades.heading(-1)
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void shouldRejectAHeadingLevelLessThanOneWithHeadingText() {
		ReadyMades.heading(-1, "Don't do it!")
	}
	
	@Test
	void shouldReturnTheBodyElementWhenCreatingAnEntirePage() {
		def returnedNode = ReadyMades.page()
		assertThat(returnedNode.getLocalName(), is(equalTo("body")))
	}
	
	@Test
	void shouldCreateAnEntirePage() {
		def page = ReadyMades.page();
		def html = page.query("/html:html", context)
		assertThat(html.size(), is(1))
		assertThat(html.get(0).getChildCount(), is(2))
		
		assertThat(page.query("/html:html/html:head", context).size(), is(1))
		assertThat(page.query("/html:html/html:body", context).size(), is(1))
		
		assertThat(html.get(0).getChildElements().get(0).getLocalName(), is("head"))
		assertThat(html.get(0).getChildElements().get(1).getLocalName(), is("body"))
	}
}
